package cn.azure.netty.serializable.msgpack;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.msgpack.MessagePack;
import org.msgpack.jackson.dataformat.MessagePackMapper;

import java.util.List;

/*基于MessagePack的解码器，反序列化*/
public class MsgPackJacksonDecoder extends MessageToMessageDecoder<ByteBuf> {
    private final ObjectMapper objectMapper = new MessagePackMapper();
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
            throws Exception {
        final int length = msg.readableBytes();
        final byte[] array = new byte[length];
        msg.getBytes(msg.readerIndex(),array,0,length);
        User user = objectMapper.readValue(array, User.class);
        out.add(user);
    }
}
